package cn.uexpo.lhz.controller.admin;

import java.text.SimpleDateFormat;
import java.util.*;

import cn.uexpo.lhz.biz.EventActivityBiz;
import cn.uexpo.lhz.biz.EventLabelBiz;
import cn.uexpo.lhz.common.oldUtils.PageRequest;

import cn.uexpo.lhz.domain.*;

import cn.uexpo.lhz.domain.vo.EventVo;
import cn.uexpo.lhz.plus.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONArray;

import cn.uexpo.lhz.biz.EventBiz;
import cn.uexpo.lhz.common.MqttGateway;
import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.common.oldUtils.PageResult;
import cn.uexpo.lhz.common.utils.JsonData;
import cn.uexpo.lhz.common.utils.StringUtil;
import cn.uexpo.lhz.core.http.HttpResult;


/**
 * 人员管理
 */


@RestController
@RequestMapping("/cert/event")
public class EventController extends BaseController<Event> {

    @Autowired
    private EventBiz eventBiz;

    @Autowired
    private MqttGateway mqttGateway;//等于网关的作用

    @Autowired
    private EventLabelBiz eventLabelBiz;

    @Autowired
    private EventActivityBiz eventActivityBiz;

    /**
     * 查询项目名称
     *
     * @return
     */
    @GetMapping("/findEventName")
    public R findEventName() {
        List<Map> list = eventBiz.selectEventName();
        return R.ok().put("event", list);
    }


    /**
     * 查询所有项目有分页(原生)
     *
     * @param
     * @return
     */
    @GetMapping("/findEventAll")
    public HttpResult findEventALL(@RequestParam Map<String, Object> params) {

        //		String name=request.getParameter("name");
        EventExample example = new EventExample();
        EventExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank((String) params.get("name"))) {
            criteria.andNameLike("%" + (String) params.get("name") + "%");//按项目名模糊查询
        }
        Event event = new Event();
        event.setName((String) params.get("name"));
        String page = (String) params.get("page");
        String limit = (String) params.get("limit");
        event.setPage(Integer.parseInt(page));
        event.setLimit(Integer.parseInt(limit));

        example.setOrderByClause("createTime desc");
        PageResult result = eventBiz.findPage(event, example);
        return HttpResult.ok(result);
    }


    @RequestMapping("/findEventActivityAll3")
    public Object findEventActivityAll3(@RequestParam Map<String, Object> params) {
        String eventId = (String) params.get("eventId");
        List<EventActivity> list = eventActivityBiz.selectActivityAndLocation(eventId);
        JsonData jsonData = new JsonData();
        jsonData.setCode(200);
        jsonData.setData(list);
        return jsonData;
    }


    /**
     * 标签下拉框
     *
     * @param
     * @return
     */

    @GetMapping("/eventLabelAll")
    public R eventLabelAll() {
        EventLabelExample example = new EventLabelExample();
        List<EventLabel> list = eventLabelBiz.selectByExample(example);
        return R.ok().put("EventLabel", list);
    }


    /**
     * 根据项目名删除单个项目信息
     *
     * @param
     * @return
     */
    @GetMapping("/deleteEventSingle")
    public R deleteEventSingle(@RequestParam String id) {
        eventBiz.deleteByID(Integer.parseInt(id));
        return R.ok();
    }

    /**
     * 根据项目ID修改单个项目信息
     *
     * @param event
     * @return
     */
    @PostMapping("/updateEventSingle")
    public R updateEventSingle(@RequestBody Event event) {
        EventExample example = new EventExample();
        EventExample.Criteria criteria = example.createCriteria();
        if (event.getId() != null) {
            criteria.andIdEqualTo(event.getId());
        }
        eventBiz.updateByExampleSelective(event, example);
        return R.ok().put("data", event.getUsable());
    }

    /**
     * 查询一个项目下的活动名称
     * 需值: 项目ID  活动名称
     *
     * @param event
     * @return
     */
    @RequestMapping("/findActivityName")
    public Object findActivityName(@RequestBody Event event) {
        eventBiz.selectByEventIdAndActivityName(event);
        return JsonData.toJsonObject("查询活动名称", "200", true, event);
    }

    /**
     * 根据项目名称查询项目的XXX
     *
     * @param event
     * @return
     */
    @RequestMapping("/findEventByName")
    public Object findEventByName(@RequestBody Event event) {
        EventExample example = new EventExample();
        EventExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(event.getName())) {
            criteria.andNameEqualTo(event.getName());
        }
        List<Event> list = eventBiz.selectByExample(example);
        return JsonData.toJsonObject("查询成功", "200", true, list);
    }
    /**
     * 下发 人员相关信息
     * @param event
     * @return
     */
	@PostMapping("/findEventStaffAll")
	public Object findEventStaffAll(@RequestBody Event event) {
		int listTotal=eventBiz.selectByEventNameCount(event);
		int pageSize = event.getLimit();
		int pageNum=0;
		if((listTotal % (pageSize))>0){
			pageNum = (int) (listTotal / (pageSize)) + 1;
		}else {
			pageNum = (int) (listTotal / (pageSize)) ;
		}

		JsonData jsonData=new JsonData();
		PageRequest pageRequest=new PageRequest();
		Map<String,Object> map = new HashMap<>();

		if(!"".equals(event.getStaffFeature())&&null!=event.getStaffFeature()&&!"".equals(event.getLimit())&&0!=event.getLimit()){
			if("下发所有人员信息".equals(event.getStaffFeature())){
				//生成pagenum
				for(int i=1; i<=pageNum;i++){
					pageRequest.setPage(i);
					pageRequest.setLimit(pageSize);
					PageResult event2=eventBiz.selectByEventName(pageRequest,event);
					map.put("msg", 200);
					map.put("data", event2.getContent());
					//map转成json数据
					JSONArray jArray = new JSONArray();
					jArray.add(map);
					String data=jArray.toString();
					//把传递数据放入mq当中(主题方式)
					mqttGateway.sendToMqtt(data,"staffMsg");//发送staffMsg主题
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				jsonData.setCode(200);
				jsonData.setData(listTotal);
				jsonData.setMessage("下发所有人员信息成功");
			}else if("下发无特征值人员信息".equals(event.getStaffFeature())){
				//生成pagenum
				for(int i=1; i<=pageNum;i++){
					pageRequest.setPage(i);
					pageRequest.setLimit(pageSize);
					PageResult event2=eventBiz.selectByEventName2(pageRequest,event);
					map.put("msg", 200);
					map.put("data", event2.getContent());
					//map转成json数据
					JSONArray jArray = new JSONArray();
					jArray.add(map);
					String data=jArray.toString();
					//把传递数据放入mq当中(主题方式)
					mqttGateway.sendToMqtt(data,"staffMsg");//发送staffMsg主题
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				jsonData.setCode(200);
				jsonData.setData(listTotal);
				jsonData.setMessage("下发无特征值人员信息成功");
			}
		}
		return jsonData;
	}
    //有无特征值都含的人员信息
/*	@RequestMapping("/findEventStaffAll")
	@ResponseBody
	public Object findEventStaffAll(@RequestBody Event event) {
		int listTotal=eventBiz.selectByEventNameCount(event);
		int pageSize = 200;
		int pageNum=0;
		if((listTotal % (pageSize))>0){
			pageNum = (int) (listTotal / (pageSize)) + 1;
		}else {
			pageNum = (int) (listTotal / (pageSize)) ;
		}
		//生成pagenum
		for(int i=1; i<=pageNum;i++){
			PageRequest pageRequest=new PageRequest();
			pageRequest.setPageNum(i);
			pageRequest.setPageSize(pageSize);
			PageResult event2=eventBiz.selectByEventName(pageRequest,event);
			Map<String,Object> map = new HashMap<>();
			map.put("msg", 200);
			map.put("data", event2.getContent());
			//map转成json数据
			JSONArray jArray = new JSONArray();
			jArray.add(map);
			String data=jArray.toString();
			//把传递数据放入mq当中(主题方式)
			mqttGateway.sendToMqtt(data,"staffMsg");//发送staffMsg主题
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		JsonData jsonData=new JsonData();
		jsonData.setCode(200);
		jsonData.setData(listTotal);
		jsonData.setMessage("下发人员信息成功");
		return jsonData;
	}
	//只含无特征值的人员信息
	@RequestMapping("/findEventStaffAll2")
	@ResponseBody
	public Object findEventStaffAll2(@RequestBody Event event) {
		int listTotal=eventBiz.selectByEventNameCount(event);
		int pageSize = 200;
		int pageNum=0;
		if((listTotal % (pageSize))>0){
			pageNum = (int) (listTotal / (pageSize)) + 1;
		}else {
			pageNum = (int) (listTotal / (pageSize)) ;
		}
		//生成pagenum
		for(int i=1; i<=pageNum;i++){
			PageRequest pageRequest=new PageRequest();
			pageRequest.setPageNum(i);
			pageRequest.setPageSize(pageSize);
			PageResult event2=eventBiz.selectByEventName2(pageRequest,event);

			Map<String,Object> map = new HashMap<>();
			map.put("msg", 200);
			map.put("data", event2.getContent());
			//map转成json数据
			JSONArray jArray = new JSONArray();
			jArray.add(map);
			String data=jArray.toString();
			//把传递数据放入mq当中(主题方式)
			mqttGateway.sendToMqtt(data,"staffMsg");//发送staffMsg主题
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		JsonData jsonData=new JsonData();
		jsonData.setCode(200);
		jsonData.setData(listTotal);
		jsonData.setMessage("下发无特征值人员信息成功");
		return jsonData;
	}*/
    //增量下发人员信息
	@RequestMapping("/findEventStaffAll3")
	@ResponseBody
	public Object findEventStaffAll3(@RequestBody EventVo event) {
		int listTotal=eventBiz.selectByEventNameCount2(event);
		int pageSize = 500;
		int pageNum=0;
		if((listTotal % (pageSize))>0){
			pageNum = (int) (listTotal / (pageSize)) + 1;
		}else {
			pageNum = (int) (listTotal / (pageSize)) ;
		}
		//生成pagenum
		for(int i=1; i<=pageNum;i++){
			PageRequest pageRequest=new PageRequest();
			pageRequest.setPage(i);
			pageRequest.setLimit(pageSize);
			PageResult event2=eventBiz.selectByEventName3(pageRequest,event);
			Map<String,Object> map = new HashMap<>();
			map.put("msg", 200);
			map.put("data", event2.getContent());
			//map转成json数据
			JSONArray jArray = new JSONArray();
			jArray.add(map);
			String data=jArray.toString();
			//把传递数据放入mq当中(主题方式)
			mqttGateway.sendToMqtt(data,"staffMsg");
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		JsonData jsonData=new JsonData();
		jsonData.setCode(200);
		jsonData.setData(listTotal);
		jsonData.setMessage("增量下发人员信息成功");
		return jsonData;
	}

    /**
     * 下发项目相关信息
     *
     * @param event
     * @return
     */
    @RequestMapping("/findEventDependencyAll")
    @ResponseBody
    public Object findEventDependencyAll(@RequestBody Event event) {
        List<Event> list = eventBiz.selectEventDependencyAllByEventID(event);
        Map<String, Object> map = new HashMap<>();
        map.put("msg", 200);
        map.put("data", list);
        //map转成json数据
        JSONArray jArray = new JSONArray();
        jArray.add(map);
        String data = jArray.toString();
        //把传递数据放入mq当中(主题方式)
        mqttGateway.sendToMqtt(data, "EventDependencyMsg");//发送EventDependencyMsg主题
        return data;
    }

    /**
     * 添加单个项目
     *
     * @param event
     * @return
     * @throws Exception
     */
    @RequestMapping("/addEventSingle")
    @ResponseBody
    public Object addEventSingle(@RequestBody Event event) throws Exception {
        Date date = new Date();     //获取一个Date对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   //创建一个格式化日期对象
        String time = simpleDateFormat.format(date);   //格式化后的时间
        Date times = simpleDateFormat.parse(time); //String-->Date
        event.setCreatetime(times);
        event.setUsable(0);
        event.setIsshow(0);
        eventBiz.insert(event);
        return JsonData.toJsonObject("项目新增成功", "200", true, event);
    }


    /**
     * 查询所有项目无分页
     */
    //	@RequestMapping("/findEventALL2")
    //	@ResponseBody
//    	public Object findEventALL2(@RequestBody Event event) {
    //		EventExample example=new EventExample();
    //		EventExample.Criteria criteria=example.createCriteria();
    //		if(StringUtil.isNotBlank(event.getName())) {
    //			criteria.andNameLike("%"+event.getName()+"%");//按项目名模糊查询
    //		}
    //		example.setOrderByClause("createTime desc");
    //
    //		List<Event> list=eventBiz.selectByExample(example);
    //		return JsonData.toJsonObject("查询成功", "200", true, list);
    //	}

    /**
     * event人数限制操作按钮
     *
     * @param event
     * @return
     */
    @PostMapping("/eventState")
    public Map<String, Object> eventState(@RequestBody Event event) {
        Map<String, Object> map = new HashMap<String, Object>();
        int no = 0;
        String code = "500";
        String msg = "失败";
        //		String name=request.getParameter("name");
        EventExample example = new EventExample();
        EventExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(event.getName()) && null != event.getId()) {
            criteria.andNameEqualTo(event.getName());
            criteria.andIdEqualTo(event.getId());
        }
        List<Event> eventList = eventBiz.selectByExample(example);
        if (null != eventList && eventList.size() > 0) {
            no = eventBiz.updateByPrimaryKeySelective(event);
        }
        if (1 == no) {
            code = "200";
            msg = "成功";
        }
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }


}
